package com.wsllx.springboot.server;

import com.wsllx.springboot.interfac.WebServer;
import org.apache.catalina.*;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * @program: shiguang-springboot
 * @description: Tomcat容器
 * @author: liulx
 * @create: 2022-07-17 20:24
 */
public class TomcatWebServer implements WebServer, ApplicationContextAware {
	private WebApplicationContext applicationContext;
	@Override
	public void start() {
		System.out.println("启动TomcatWebServer");
		startTomcat(applicationContext);
	}

	/**
	 * 启动TomCat
	 * @param applicationContext
	 */
	public static void startTomcat(WebApplicationContext applicationContext){
		
		Tomcat tomcat = new Tomcat();
		
		Server server = tomcat.getServer();
		Service service = server.findService("Tomcat");
		
		Connector connector = new Connector();
		connector.setPort(8081);
		
		Engine engine = new StandardEngine();
		engine.setDefaultHost("localhost");
		
		Host host = new StandardHost();
		host.setName("localhost");
		
		String contextPath = "";
		Context context = new StandardContext();
		context.setPath(contextPath);
		context.addLifecycleListener(new Tomcat.FixContextListener());
		
		host.addChild(context);
		engine.addChild(host);
		
		service.setContainer(engine);
		service.addConnector(connector);
		
		tomcat.addServlet(contextPath, "dispatcher", new DispatcherServlet(applicationContext));
		context.addServletMappingDecoded("/*", "dispatcher");
		
		try {
			tomcat.start();
		} catch (LifecycleException e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * 启动spring容器
	 * @param applicationContext
	 * @throws BeansException
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext= (WebApplicationContext) applicationContext;
	}
}
